#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "head.h"
#define BUFFER_SIZE 1024  
#define FILE_NAME_MAX_SIZE 512 

static void usage(const char* proc)
{
    printf("%s[server_ip][server_port]\n", proc);
}

//./client server_ip, server_port
int main(int argc, char* argv[])
{
    if(argc != 3)
    {
        usage(argv[0]);
        return 1;
    }
    //1.创建sock
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if(sock < 0)
    {
        perror("socket");
        return 2;
    }
    //2.connect
    struct sockaddr_in server;
    server.sin_family = AF_INET;
    server.sin_port = htons(atoi(argv[2]));
    //将点分十进制的字符串转换成能在网络上传输的网络号
    server.sin_addr.s_addr = inet_addr(argv[1]);
    //调用connect,第一个参数是客户端的socket套接字，第二个参数是服务器端套接字
    if(connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0)
    {
        perror("connect");
        return 2;
    }
    /*//先写后读
    char buf[1024];
    while(1)
    {
        printf("Please enter# ");
        fflush(stdout);
        //读取标准输入键盘中的数据
        ssize_t s = read(0, buf, sizeof(buf) - 1);
        if(s > 0)
        {
            buf[s - 1] = 0;
            //将buf中的内容写到套接字中
            write(sock, buf, strlen(buf));
            //读取服务器的响应
            ssize_t _s = read(sock, buf, sizeof(buf)-1);
            if(_s > 0)
            {
                buf[_s] = 0;
                printf("server ech0# %s\n", buf);
            }
        }
    }*/
    char file_name[FILE_NAME_MAX_SIZE+1];  
    bzero(file_name, FILE_NAME_MAX_SIZE+1);  
    printf("Please Input File Name On Client: ");
    scanf("%s", file_name);  
    char buffer[BUFFER_SIZE];  
    bzero(buffer, BUFFER_SIZE);  
    strncpy(buffer, file_name, strlen(file_name)>BUFFER_SIZE?BUFFER_SIZE:strlen(file_name));  
    if(send(sock, buffer, BUFFER_SIZE, 0) < 0)  
    {
	perror("Send File Name Failed!");
	exit(1);  
    }
    FILE *fp = fopen(file_name, "r");  
    if(NULL == fp)  
    {  
       	printf("File: %s Not Found!\n", file_name);  
    }  
    else  
    {  
       	bzero(buffer, BUFFER_SIZE);  
       	int length = 0;  
       	while((length = fread(buffer, sizeof(char), BUFFER_SIZE, fp)) > 0)
       	{
    	    if(send(sock, buffer, length, 0) < 0)  
            {  
             	printf("Send File: %s Failed!/n", file_name);  
                break;  
            }  
            bzero(buffer, BUFFER_SIZE);  
        }  
	printf("Send File: %s Successful!\n", file_name);
	printf("The File has %d words.\n",wcfunc(file_name));	  
    }
    fclose(fp);
    close(sock);
    return 0;
}

int wcfunc(char *file_name)
{
	int t;
	int w = 0;
	int state = 0;
	FILE *in;
	if((in = fopen(file_name,"r"))==NULL)
	{
		printf("wc %s:no this file or dir\n",file_name);
		return 0;
	}
	while((t=fgetc(in))!=EOF)
	{
		
		if(t=='\n'||t==' '||t=='\r') {
            		state = 0;
            		continue;
        	} else {
            		if(state == 0) {
                	state = 1;
                	w++;
           		}
            		continue;
        	}
	}
	return w;
}
